//
//  XHMethodPaymentViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/18.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHMethodPaymentViewController.h"

#import "Services.h"

#import "XHMethodPaymentCell.h"

@interface XHMethodPaymentViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSDictionary * m_userDic;
@end

@implementation XHMethodPaymentViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.ui_tableView.scrollEnabled = NO;
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHMethodPaymentCell" bundle:nil] forCellReuseIdentifier:XHMethodPaymentCellIdentifier];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getUserInfo];
}

- (void)getUserInfo
{
    WS(weakSelf)
    self.m_userDic=[ServiceManager getUserInfoSuccess:^(NSDictionary *data) {
        weakSelf.m_userDic = data;
        
        [weakSelf.ui_tableView reloadData];
    } failure:^{
        
    }];
    [self.ui_tableView reloadData];
}

- (IBAction)cancelAction:(id)sender
{
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}

- (void)selectedPayMethod:(NSString *)payMethod
{
    if ([self.delegate respondsToSelector:@selector(didSelectedMethodPayment:)]) {
        [self.delegate didSelectedMethodPayment:payMethod];
    }
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger number = 1;
    return number;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows = 3;
    
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = nil;
    
    cell = [self methodPaymentCell:indexPath];
    
    return cell;
}

- (XHMethodPaymentCell *)methodPaymentCell:(NSIndexPath*)indexPath
{
    XHMethodPaymentCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHMethodPaymentCellIdentifier];
    
    if (indexPath.row==0)
    {
        cell.ui_payImage.image = [UIImage imageNamed:@"shouye_icon_weixin"];
        cell.ui_payName.text = @"微信支付";
        cell.ui_balance.hidden = YES;
    }
    else if (indexPath.row==1)
    {
        cell.ui_payImage.image = [UIImage imageNamed:@"shouye_icon_zhifubao"];
        cell.ui_payName.text = @"支付宝支付";
        cell.ui_balance.hidden = YES;
    }
    else if (indexPath.row==2)
    {
        cell.ui_payImage.image = [UIImage imageNamed:@"shouye_icon_yue"];
        cell.ui_payName.text = @"余额支付";
        cell.ui_balance.hidden = NO;
        cell.ui_balance.text = [NSString stringWithFormat:@"余额可用￥%.2f",[self.m_userDic[@"cashoutBalance"] doubleValue]];
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row==0)
    {
        NSLog(@"微信");
        [self selectedPayMethod:kPayChannelWX];
    }
    else if (indexPath.row==1)
    {
        NSLog(@"支付宝");
        [self selectedPayMethod:kPayChannelAlipay];
    }
    else if (indexPath.row==2)
    {
        NSLog(@"余额");
        NSLog(@"余额:%@",self.m_userDic[@"cashoutBalance"]);
        NSLog(@"需支付:%@",self.m_integral);
        if([self.m_userDic[@"cashoutBalance"] doubleValue]>=[self.m_integral doubleValue])
        {
            [self selectedPayMethod:kPayChannelYE];
        }
        else
        {
            [PromptAction showInfoPrompt:@"账户余额不足!"];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
